package methodReference;

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

public class MainTest {
    public static void main(String[] args) {
        Student s1 = new Student(20, "maomao");
        Student s2 = new Student(60, "keke");
        Student s3 = new Student(50, "jiji");
        Student s4 = new Student(10, "mimi");
        List<Student> studentList = Arrays.asList(s1, s2, s3, s4);

        // lambda表达式与方法引用：
//        studentList.forEach(student -> System.out.println(student));
//        studentList.forEach(System.out::println);

        // lambda即可以表述简单操作，也可以在方法体内进行复杂的操作，
        // 然后并不是所有的操作都是直接写在lambda表达式函数体内的，
        // 我们可以语法糖直接引用方法（复杂操作需要在具体引用的方法中）
//        studentList.forEach(student -> {
//            // 更复杂的操作
//            System.out.println(student.getName());
//            System.out.println(student.getScore());
//        });


        // 本质都是lambda表达式，都必须依托与类，都是作为函数式接口的的实现。
//        Consumer<Student> studentConsumer1 = student -> System.out.println(student);
//        Consumer<Student> studentConsumer2 = System.out::println;
//        System.out.println(studentConsumer1); // methodReference.MainTest$$Lambda$4/1452126962@5fd0d5ae
//        System.out.println(studentConsumer2); // methodReference.MainTest$$Lambda$5/931919113@2d98a335

        /**
         * 方法引用：
         * 类::静态方法
         */
//        studentList.sort((studentParam1, studentParam2) -> Student.comepareStudentByScoue(studentParam1, studentParam2));
//        studentList.forEach(student -> System.out.println(student.getScore()));

        // 注释上面的排序，防止对下面排序影响
//        studentList.sort(Student::comepareStudentByScoue);
//        studentList.forEach(student -> System.out.println(student.getScore()));

//        studentList.sort((studentParam1, studentParam2) -> Student.comepareStudentByName(studentParam1, studentParam2));
//        studentList.forEach(student -> System.out.println(student.getName()));

//        studentList.sort(Student::comepareStudentByName);
//        studentList.forEach(student -> System.out.println(student.getName()));

//        Comparator<Student> comparator = Student::comepareStudentByName;
//        System.out.println(comparator);

        /**
         * 方法引用：
         * 类实例::实例方法
         */
//        CompareStudent compareStudent = new CompareStudent();
//        studentList.sort(compareStudent::compareStudentByScore);
////        studentList.sort(compareStudent::compareStudentByName);
//        studentList.forEach(student -> System.out.println(student.getScore()));

        /**
         * 方法引用
         * 类::实例方法
         */
//  //      studentList.sort((studentParam1, studentParam2) -> Student.comepareStudentByScoue(studentParam1, studentParam2));
        studentList.sort(Student::compareStudentScore);
        studentList.forEach(student -> System.out.println(student.getScore()));

        List<String> cites = Arrays.asList("beijing", "tianjing", "shanghai", "qingdao");
        Collections.sort(cites, (cite1, cite2) -> cite1.compareToIgnoreCase(cite2));
//        cites.forEach(cite -> System.out.println(cite));

        Collections.sort(cites, String::compareToIgnoreCase);
        cites.forEach(cite -> System.out.println(cite));

        MainTest mainTest = new MainTest();
        System.out.println(mainTest.getStr(String::new));// 点击new自动定位到无参数的构造函数

        System.out.println(mainTest.getString("hello", String::new)); // 点击new自动定位到有参数的构造函数
    }

    public String getStr(Supplier<String> stringSupplier) {
        return "hey" + stringSupplier.get();
    }

    public String getString(String hello, Function<String, String> function) {
        return function.apply(hello);
    }
}
